<?php
/*
Copyright (C) 2009 de Flotte Maxence

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
?>
<?php
//ZzMenu
class std_Menu extends Bout {
	protected $typeItem = 'rub';
	private $innerHTML;
	protected $url = 'std/menu.php';
	protected $bootName = 'std_Menu';

	public function setTypeItem($arg) {
		$this->typeItem = $arg;
	}
	private function buildStaticMenu() {
		$this->innerHTML = null;
		//On recupère la liste des rubriques
		$MySql = $GLOBALS['MySql'];
		$req = 'SELECT L.id,L.name,L.user,L.group, L.userR, L.groupR, L.otherR FROM '.$GLOBALS['prefix'].'label As L LEFT JOIN '.$GLOBALS['prefix'].'_label As _L ON L.id = _L.id_son WHERE _L.id_father=0 AND L.type LIKE \'%'.$this->typeItem.'\' Order By `index`,name;';
		$query = $MySql->query($req);
		//On récupère le label
		$_Label = explode('-',$_GET['label']);
		$label = intval($_Label[0]);
		$_Racine = array_reverse($_Label);
		$racine = intval($_Racine[0]);
		//On génère le menu
		while ($ligne = $query->fetch_assoc()) {
			$this->innerHTML[$ligne['name']] = array($ligne['id'],$ligne['user'],$ligne['group'],$ligne['userR'],$ligne['groupR'],$ligne['otherR'],array(),array());
			//On liste les rubriques si elle sont accessible en lecture
			if(isReadable($ligne['user'],$ligne['group'],$ligne['userR'],$ligne['groupR'],$ligne['otherR']))
	if($ligne['id'] == $racine) {
		//$li .= '<li class="selected"><a href="?label='.$ligne['id'].'">'.$ligne['name'].'</a></li>';
		//On liste le chemin
		foreach(array_reverse($_Label) As $key => $ssLabel)
			if($key != 0) {
				$req = 'SELECT id,name,user,`group`,userR,groupR,otherR FROM '.$GLOBALS['prefix'].'label WHERE id = '.intval($ssLabel).' AND type LIKE \'%'.$this->typeItem.'\';';
				
				$query2 = $MySql->query($req);
				$ssLigne = $query2->fetch_assoc();
				$chemin = null;
				for($i=$key;$i>0;$i--)
		$chemin .= $_Racine[$i].'-';
				$chemin .= $_Racine[0];
				$this->innerHTML[$ligne['name']][6][$ssLigne['name']] = array($chemin,$ssLigne['user'],$ssLigne['group'],$ssLigne['userR'],$ssLigne['groupR'],$ssLigne['otherR']);
				//if($ssLigne != null and isReadable($ssLigne['user'],$ssLigne['group'],$ssLigne['userR'],$ssLigne['groupR'],$ssLigne['otherR']))
		//$liChemin .= '<li class="chemin"><a href="?label='.$chemin.'">'.$ssLigne['name'].'</a></li>';
			}

		//On liste les sous-labels
		$req = 'SELECT L.id,L.name,L.user,L.group,L.userR, L.groupR,L.otherR FROM '.$GLOBALS['prefix'].'label As L INNER JOIN '.$GLOBALS['prefix'].'_label As _L ON L.id = _L.id_son WHERE _L.id_father = '.$label.' AND L.type LIKE \'%'.$this->typeItem.'\' Order By `index`,name;';
		$query2 = $MySql->query($req);
		while($ssLigne = $query2->fetch_assoc()) {
			$this->innerHTML[$ligne['name']][7][$ssLigne['name']] = array($ssLigne['id'].'-'.$_GET['label'],$ssLigne['user'],$ssLigne['group'],$ssLigne['userR'],$ssLigne['groupR'],$ssLigne['otherR']);
			//if($ssLigne != null and isReadable($ssLigne['user'],$ssLigne['group'],$ssLigne['userR'],$ssLigne['groupR'],$ssLigne['otherR']))
				/*if($label==$ssLigne['id'])
		$liList .= '<li class="ssLabel-selected"><a href="?label='.$ssLigne['id'].'-'.$_GET['label'].'">'.$ssLigne['name'].'</a></li>';
				else
		$liList .= '<li class="ssLabel"><a href="?label='.$ssLigne['id'].'-'.$_GET['label'].'">'.$ssLigne['name'].'</a></li>';	*/
		}
		//$li .= '<ul>'.$liChemin.'<ul>'.$liList.'</ul></ul>';

		//On liste les sous-labels
		/*$req = 'SELECT L.id,L.name FROM ZzLabel As L LEFT JOIN Zz_Label As _L ON L.id = _L.id_son WHERE _L.id_son IS NULL;';
		$query2 = $MySql->query($req);
		while ($ssRubrique = $query->fetch_assoc())*/
	}
	//else
		//$li .= '<li><a href="?label='.$ligne['id'].'">'.$ligne['name'].'</a></li>';

		}
//echo serialize($this->inner);
//echo '<pre>';
//print_r($this->innerHTML);
//print_r($this->inner);
//echo '</pre>';
		//return $li;
		$this->inner = serialize($this->innerHTML);
	}

	public function buildMenu() {

		$this->inner = unserialize($this->inner);
		$_Label = explode('-',$_GET['label']);
		$label = intval($_Label[0]);
		$_Racine = array_reverse($_Label);
		$racine = intval($_Racine[0]);
		$li = null;
		$liChemin = null;
		$liList = null;
		foreach($this->inner as $key => $rub) {
			if(isReadable($rub[1],$rub[2],$rub[3],$rub[4],$rub[5]))
				if($rub[0] == $racine) {
					$li .= '<li class="selected"><a href="?label='.$rub[0].'">'.$key.'</a></li>';
					foreach($rub[6] as $keyC => $chemin)
						if(isReadable($chemin[1],$chemin[2],$chemin[3],$chemin[4],$chemin[5]))
							$liChemin .= '<li class="chemin"><a href="?label='.$chemin[0].'">'.$keyC.'</a></li>';
					foreach($rub[7] as $keyC => $ssRub)
						if(isReadable($ssRub[1],$ssRub[2],$ssRub[3],$ssRub[4],$ssRub[5]))
							if($label==$ssRub[0])
								$liList .= '<li class="ssLabel-selected"><a href="?label='.$ssRub[0].'-'.$_GET['label'].'">'.$ssRub[0].'</a></li>';
							else
								$liList .= '<li class="ssLabel"><a href="?label='.$ssRub[0].'">'.$keyC.'</a></li>'; 
					$li .= '<ul>'.$liChemin.'<ul>'.$liList.'</ul></ul>';
				}
			else
				$li .= '<li><a href="?label='.$rub[0].'">'.$key.'</a></li>';
		}
		return $li;
	}

	public function build() {
		//On construit le menu
		if($this->inner == null)
			$this->buildStaticMenu();
		return '	<ul id="'.$this->name.'" class="ZzMenu"">'.$this->buildMenu().' </ul>'."\n";
	}
	public function buildStatic() {
		$this->buildStaticMenu();
		$this->addAutoSet('typeItem');
		return $this->Me();
		//return '<ZzO object="std_Menu" id="'.$this->name.'">'.serialize($this->innerHTML).'</ZzO>';
	}
}
?>